<?php
// script ktory zalohuje databazu a zdrojaky a posiela to na maily
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");

$serverDB = 'localhost';
$loginDB = 'falkbmb';
$nameDB = 'falkbmb';
$passwordDB = 'lsojhrnm';

$FTP_ROOT = '/home/pambmb/htdocs';
$FTP_FOLDER = 'falkensteiner/';
$FILE = $FTP_ROOT . '/' . $FTP_FOLDER;

$file = 'db-backup-falk';
$cesta = $FILE . 'dbBackup/' . $file . '.sql';

$backupFolder = '/home/payrollbackup/wejg645LLOOSDAwen4554erg/';

include 'phpmailer/class.phpmailer.php';
include 'inc/functions.php';

// zmazanie suborov v priecinku zaloh
//$folder = dir($backupFolder);
//while($f = $folder->read()){
  //  if ($f=="." || $f=="..") continue;
  //  unlink($backupFolder.$f);
//}
//////////////////////////////////////
// zazipovanie zdrojakov

$dirX = getcwd();
chdir($FTP_ROOT);

$outfile = "cfalk" .date("dmY-H:i:s");
$outfile_name = $backupFolder .$outfile;
$infile = $FTP_FOLDER;

system("zip -r -P $passwordDB $outfile_name  $infile -x $infile"."docs\*");
chdir($dirX);

////////////////////////////////////////
// poslanie mailov aj so zalohami kodov

$text = '<h3>Dobrý deň!</h3>';
$text .= '<p>V priečinku '.$backupFolder.' je nahratá záloha zdrojových kódov.</p>';

//poslat_email('jpogadl@gmail.com', 'Payroll falkensteiner - zaloha kodov', $text);
poslat_email('sec@alfac.sk', 'Payroll falkensteiner - zaloha kodov', $text);
////////////////////////////////////////
// zaloha db

backup_tables($cesta, $serverDB, $loginDB, $passwordDB, $nameDB);

////////////////////////////////////////
// zazipovanie zalohy
$dirX = getcwd();
chdir($FILE . 'dbBackup/');

$outfile = "dbfalk_".date("dmY-H:i:s");
$outfile_name = $backupFolder .$outfile;
$infile = $file . '.sql';

system("zip -P $passwordDB $outfile_name $infile");
unlink($infile);
chdir($dirX);

////////////////////////////////////////
// poslanie mailov aj so zalohou db

$text = '<h3>Dobrý deň!</h3>';
$text .= '<p>V priečinku '.$backupFolder.' je nahratá záloha databázy.</p>';

poslat_email('sec@alfac.sk', 'Payroll falkensteiner - zaloha db', $text);
//poslat_email('jpogadl@gmail.com', 'Payroll falkensteiner - zaloha db', $text);

////////////////////////////////////////
/* backup the db OR just a table */

function backup_tables($cesta, $host, $user, $pass, $name, $tables = '*') {

    $link = mysql_connect($host, $user, $pass);
    mysql_select_db($name, $link);
    
    //get all of the tables
    if ($tables == '*') {
        $tables = array();
        $result = mysql_query('SHOW TABLES');
        while ($row = mysql_fetch_row($result)) {
            $tables[] = $row[0];
        }
    } else {
        $tables = is_array($tables) ? $tables : explode(',', $tables);
    }
    $return = '';
    //cycle through
    
    foreach ($tables as $table) {
        $result = mysql_query('SELECT * FROM ' . $table);
        $num_fields = mysql_num_fields($result);

        $return.= 'DROP TABLE IF EXISTS ' . $table . ';';
        $row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE ' . $table));
        $return.= "\n\n" . $row2[1] . ";\n\n";

        for ($i = 0; $i < $num_fields; $i++) {
            while ($row = mysql_fetch_row($result)) {
                $return.= 'INSERT INTO ' . $table . ' VALUES(';
                for ($j = 0; $j < $num_fields; $j++) {
                    $row[$j] = addslashes($row[$j]);
                    $row[$j] = str_replace("\n", "\\n", $row[$j]);
                    if (isset($row[$j])) {
                        $return.= '"' . $row[$j] . '"';
                    } else {
                        $return.= '""';
                    }
                    if ($j < ($num_fields - 1)) {
                        $return.= ',';
                    }
                }
                $return.= ");\n";
            }
        }
        $return.="\n\n\n";
    }

    //save file
    $handle = fopen($cesta, 'w+');
    fwrite($handle, $return);
    fclose($handle);
}

?>
